package org.itboys.mobile.util.getui.utils;

import com.gexin.rp.sdk.base.impl.Target;
import org.itboys.mobile.util.getui.constant.TemplateType;
import org.itboys.mobile.util.getui.getui.PushMsgToAppUtil;
import org.itboys.mobile.util.getui.getui.PushMsgToListUtil;
import org.itboys.mobile.util.getui.pojo.BaseBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 发送消息
 * 
 * @author huanglei
 * 
 */
public class SendMessageUtil {

	/**
	 * 发送消息
	 * 
	 * 直接调用sendMsg方法即可，最简单的调用要实现BaseBean中的一个构造方法
	 * 每个参数的意义，看BaseBean中的属性注释
	 * 
	 * 例如在单元测试中执行：
	 *  BaseBean bb = new BaseBean("IKerZXhvhu7rlF8L6nu6i","YBF4V08Uwc6LeFkTjS0X63","GwqXzW5M7u9DXtjcgwJGH8","http://sdk.open.api.igexin.com/apiex.htm");
		bb.setCid("20becd13a12907d441a9152e1b377b67");
		bb.setTitle("E国寿");
		bb.setText("手机号为1861069805的用户成功兑换了综合意外险、保单号为pp281615162646、保费为30、扣除积分600");
		try {
			SendMessageUtil.sendMsg(bb, null, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	 * 
	 * @param bb
	 * @param aTargets
	 *            android用户
	 * @param iTargets
	 *            IOS用户
	 * @throws Exception
	 */
	public static void sendMsg(BaseBean bb, List<Target> aTargets,
                               List<Target> iTargets) throws Exception {
		if (null == aTargets && null == iTargets) {// 给所有APP应用发送消息
			// 针对IOS用户
			bb.setPhoneType("IOS");
			PushMsgToAppUtil.sendMsgToApp(TemplateType.TRANSMISSION_TEMPLATE,
					bb);
			// 针对android用户
			bb.setPhoneType("ANDROID");
			//PushMsgToAppUtil.sendMsgToApp(TemplateType.NOTIFICATION_TEMPLATE,
			PushMsgToAppUtil.sendMsgToApp(TemplateType.TRANSMISSION_TEMPLATE,
					bb);
		} else {
			// 给android用户发送"点击通知打开应用消息"
			bb.setPhoneType("ANDROID");
			if (null != aTargets && aTargets.size() > 0) {
				PushMsgToListUtil.sendMsgToList(
						//TemplateType.NOTIFICATION_TEMPLATE, bb, aTargets);
				TemplateType.TRANSMISSION_TEMPLATE, bb, aTargets);
			}
			// 给IOS用户发送"透传消息"；
			bb.setPhoneType("IOS");
			if (null != iTargets && iTargets.size() > 0) {
				PushMsgToListUtil.sendMsgToList(
						TemplateType.TRANSMISSION_TEMPLATE, bb, iTargets);
			}
		}
	}
	
	public static void main(String[] args) {
		BaseBean baseBean = new BaseBean("mLnmSFcbUM8FL0JhKLeF59",
				"r2uUH5tMHt82mdy3fl3VV1",
				"Yi2NhjjdOr7b6nnUjFnB47",
				"http://sdk.open.api.igexin.com/apiex.htm");
		String time = new SimpleDateFormat("HH:mm:ss").format(new Date());
		baseBean.setTitle("我是title"+time);
		baseBean.setText("我是Text"+time);
		//显示type
		baseBean.setContent("{\"code\":\"Content\",\"key\":\"Content\",\"info\":\"Content\"}");
		baseBean.setPayload("{\"code\":\"1\",\"key\":\"90000\",\"info\":\"info\"}");
		baseBean.setType(2);
		baseBean.setLogo("push.png");
		baseBean.setLogoUrl("http://static.e-jiuquan.com/beauty/images/03.png");
		baseBean.setActionLocKey("");
		baseBean.setMessage("我是Message"+time);
		baseBean.setSound("order.wav");
		//baseBean.setPhoneType(phoneType);("");
		//安卓
		List<Target> alist = new ArrayList<Target>();
		Target atarget = new Target();
		atarget.setAppId("mLnmSFcbUM8FL0JhKLeF59");
		atarget.setClientId("d387c0fd957bf6f6dedcd01dedd9ba72");
		alist.add(atarget);
		List<Target> ilist = new ArrayList<Target>();
		Target itarget = new Target();
		itarget.setAppId("mLnmSFcbUM8FL0JhKLeF59");
		itarget.setClientId("931a529504d7f76dc7c91e70abec51f6");
		ilist.add(itarget);
		try {
			System.out.println("begin");
			SendMessageUtil.sendMsg(baseBean, alist, ilist);
			System.out.println("end");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}